package com.woyowodaows.activity.mine;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.woyowodaows.R;
import com.woyowodaows.activity.home.LoginActivity;
import com.woyowodaows.application.MyApplication;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.dialog.YNDialog;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ActivityManagerUtils;
import com.woyowodaows.utils.CacheUserInfoUtils;
import com.woyowodaows.utils.LoginJudge;
import com.woyowodaows.utils.SPUtils;
import com.woyowodaows.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * \
 *
 * @author haiwei
 *         设置界面
 *         2018/1/8
 */

public class SettingActivity extends BaseActivity {


    @BindView(R.id.tv_setting_version_information)
    TextView tvSettingVersionInformation;//版本信息
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    private YNDialog exitDialog;//退出登录
    @BindView(R.id.btn_setting_exit)
    Button btnTexit;



    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "设置", "", 0, 0, null);
        ActivityManagerUtils.getInstance().addActivity(SettingActivity.this);
//        boolean isLogin = (boolean) SPUtils.get(mContext, SPUtils.IS_LOGIN, false);
//        if (isLogin) {
//            btnTexit.setVisibility(View.VISIBLE);
//        } else {
//            btnTexit.setVisibility(View.GONE);
//        }
        tvSettingVersionInformation.setText("当前版本" + getLocalVersionName());
    }

    //获取版本名称
    public String getLocalVersionName() {
        String localName = "";
        try {
            PackageInfo packageInfo = mContext.getApplicationContext().getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            localName = packageInfo.versionName;
            //   LogUtils.d("版本号",localName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localName;
    }

    @OnClick({R.id.btn_setting_exit,
            R.id.rl_setting_account_security,
            R.id.rl_setting_contact_service,
            R.id.rl_setting_version_information,
            R.id.rl_setting_risk_liability,
            R.id.rl_setting_secondary_password})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_setting_account_security://账号安全
                LoginJudge.isLogin(mContext, AccountSecurityActivity.class, null);
                break;
            case R.id.rl_setting_contact_service://联系客服
//                getServiceList();
                break;
            case R.id.rl_setting_risk_liability://风险免责
//                startActivity(new Intent(mContext,TheRiskLiabilityActivity.class));
                break;
            case R.id.btn_setting_exit://退出登录
                if (exitDialog == null) {
                    exitDialog = new YNDialog(this);
                    exitDialog.setDialogListener(new YNDialog.OnDialogListener() {
                        @Override
                        public void onCancelClick(View view) {

                        }

                        @Override
                        public void onSureClick(View view) {
                            exitLogin();
                        }
                    });
                }
                exitDialog.setTitleText("确定退出账户？");
                exitDialog.show();
                break;
            case R.id.rl_setting_version_information://版本信息

                break;
            case R.id.rl_setting_secondary_password://二级密码
//                LoginJudge.isLogin(mContext,EditTwoPasswordActivity.class, null);
                startActivity(new Intent(mContext,EditPassworddActivity.class));
                break;
        }


    }

    private void exitLogin() {
        Map<String, Object> mapParameter = new HashMap<>();
        HttpAsyncTask.getInstance().onPostParam(this, "正在退出登录...", true, InterfaceUrl.LOGOUT, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        LoginJudge.clearData();
                        finish();
                        CacheUserInfoUtils.setUserInfo(null);//清空数据库数据
                        SPUtils.remove(MyApplication.getApplicationContext(), SPUtils.IS_LOGIN);
                        LoginJudge.isLogin(mContext, LoginActivity.class, null);
                        // 退出登录删除别名
                        JPushInterface.deleteAlias(mContext,202);
                        //删除 tags
                        JPushInterface.deleteTags(mContext,201,null);
                        ActivityManagerUtils.getInstance().finishAllActivity();
                    } else {
                        ToastUtil.show_short(SettingActivity.this, bean.getMessage());
                    }
                }
            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(SettingActivity.this, message);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpAsyncTask.getInstance().progressBarDialog = null;
    }

//    //获取客服列表
//    private void getServiceList() {
//        HttpAsyncTask.getInstance().onPostParam(mContext, "加载中...", true, InterfaceUrl.GET_SERVICE_LIST, ContactServiceDataBean.class, null, new HttpRequestListener() {
//            @Override
//            public void onSuccess(Object result) {
//                if (result != null) {
//                    ContactServiceDataBean bean = (ContactServiceDataBean) result;
//                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
//                        if (mContactServiceDialog == null) {
//                            mContactServiceDialog = new ContactServiceDialog(mContext);
//                        }
//                        mContactServiceDialog.setServiceList(bean.getData());
//                        mContactServiceDialog.showDialog();
//                    } else {
//                        ToastUtil.show(mContext, bean.getMessage());
//                    }
//                }
//            }
//
//            @Override
//            public void onError(String message) {
//                ToastUtil.show(mContext, message);
//            }
//        });
//    }

}
